<template>
  <div class="address">
    <NavBar title="收货地址"></NavBar>
    <div class="address-item" v-for="item in list" :key="item.id">
      <div class="item-info">
        <div class="item-name">
          {{ item.name }} {{ item.phone }}
          <span v-if="item.default == 1">默认地址</span>
        </div>
        <div class="item-position">{{ item.address }}</div>
      </div>
      <div class="item-operate">
        <div class="item-btn">x</div>
        <div class="item-btn" @click="delAddress(item.id)">x</div>
      </div>
    </div>

    <div class="add-btn" @click="jumpAdd">添加新地址</div>
  </div>
</template>

<script >
import NavBar from "../components/NavBar";
export default {
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    jumpAdd() {
      this.$router.push({ name: "AddAddress" });
    },
    // 获取数据
    fetchData() {
      this.$axios.get("/address/address").then((res) => {
        this.list = res.data.data;
      });
    },
    // 删除
    delAddress(id) {
      this.$axios.delete("/address/address", { params: { id } }).then((res) => {
        if (res.data.code == 200) {
          this.$toast.success(res.data.msg);
          this.fetchData();
        } else {
          this.$toast.fail(res.data.msg);
        }
      });
    },
  },
  components: {
    NavBar,
  },
};
</script>

<style lang="scss" scoped>
.address {
  background-color: #f0f0f0;
  padding-top: 1.1728rem;
  padding-bottom: 1.4928rem;
  .address-item {
    padding: 0.693333rem 0.346667rem;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .item-info {
      .item-name {
        font-size: 0.453333rem;
        color: #222222;
        span {
          background-color: #e5efff;
          color: #6891f3;
          font-weight: 400;
          font-size: 0.32rem;
          padding: 0.133333rem 0.266667rem;
          border-radius: 0.08rem;
          border: 1px solid #fff;
          margin-left: 0.133333rem;
        }
      }
      .item-position {
        color: #757777;
        font-size: 0.346667rem;
        padding-top: 0.346667rem;
      }
    }
    .item-operate {
      display: flex;
      .item-btn {
        width: 0.88rem;
        height: 0.88rem;
        line-height: 0.88rem;
        font-size: 30px;
        text-align: center;
        border: 1px solid #fff;
        margin-left: 0.333333rem;
      }
    }
  }
  .add-btn {
    width: 9.066667rem;
    height: 1.52rem;
    line-height: 1.52rem;
    text-align: center;
    color: #fff;
    background: linear-gradient(0deg, #4d79ef, #6a95f4);
    border-radius: 0.08rem;
    font-size: 0.433333rem;
    margin: 0.746667rem auto;
  }
}
</style>